#include <p24FJ32GA002.h>
#include <stdio.h>
/*
 * Shift Register, afkorting: SR
 */

// TRIS
#define SR_STROBE_TRIS_1 		TRISBbits.TRISB8

// LAT
#define SR_STROBE_1			LATBbits.LATB8

// DataSR om data te bewaren voor verschillende handelingen
unsigned int DataSR_1;
unsigned int BarData;
unsigned int BarData1;
unsigned int BarData2;
unsigned int BarData3;
unsigned int BarData4;

/*
void _ISR _SPI1Interrupt(void){
SR_STROBE_1 = 1;
SPI1STATbits.SPIEN = 0;
IFS0bits.SPI1IF = 0;
SPI1STATbits.SPIEN = 1;
IEC0bits.SPI1IE = 0;

}
*/

void InitSPI_1(void)
{
	SR_STROBE_TRIS_1 = 0; // STROBE, make output
	SR_STROBE_1 = 0; // Init outputs pins
	
	// Assign functions to pins
	RPOR3bits.RP7R = 7; // SDO2
	RPOR3bits.RP6R = 8; // SCK2OUT
	
	// Schone lijn
	SPI1STAT = 0;
	IFS0bits.SPI1IF = 0;
	// 101 = Interrupt when the last bit is shifted out of SPIxSR, now the transmit is complete
	SPI1STATbits.SISEL0 = 1; 
	SPI1STATbits.SISEL1 = 0; 
	SPI1STATbits.SISEL2 = 1; 
	
	SPI1CON1bits.MSTEN = 1; // 1 = Master mode
	SPI1CON1bits.CKE = 1; //1 = Serial output data changes on transition from active clock state to Idle clock state (see bit 6)
	
	SPI1CON1bits.MODE16	= 1;
	IFS0bits.SPI1IF = 0;	// interrupt flag
	IEC0bits.SPI1IE = 0;	// interrupt enable
	
	
	SPI1CON2bits.SPIBEN = 1; // enhanched buffer SPI
	SPI1STATbits.SPIEN = 1; // spi enable
}

unsigned int SRPut_1() // SPI2Put
{
SR_STROBE_1 = 0;

while(SPI1STATbits.SPITBF);
    SPI1BUF = BarData4;
while(SPI1STATbits.SPITBF);
    SPI1BUF = BarData3;
while(SPI1STATbits.SPITBF);
    SPI1BUF = BarData2;
while(SPI1STATbits.SPITBF);
    SPI1BUF = BarData1;
while(SPI1STATbits.SPITBF);
while(!SPI1STATbits.SRMPT);
SR_STROBE_1 = 1;
    return 0;
}

void Flash_Bars(){

BarData1	= 0xFFFF;
BarData2	= 0xFFFF;
BarData3	= 0xFFFF;
BarData4	= 0xFFFF;
SRPut_1();
LongDelay(7000);
REGEL2();
PUTS_CHAR("- Shiftout LED's   ");
LongDelay(1000);
BarData1	= 0;
SRPut_1();
LongDelay(2000);
BarData2	= 0;
SRPut_1();
LongDelay(2000);
BarData4	= 0;
SRPut_1();
LongDelay(2000);
BarData3	= 0;
SRPut_1();
LongDelay(2000);

}

void BarKeuzeAct(Data, BarNr){

	switch (Data){
	
		case 1: 
		BarData = 1 << 8;
		break;
		
		case 2:
		BarData = 1 << 9;
		break;
		
		case 3:
		BarData = 1 << 10;
		break;
		
		case 4:
		BarData = 1 << 11;
		break;
		
		case 5:
		BarData = 1 << 12;
		break;
		
		case 6:
		BarData = 1 << 13;
		break;
		
		case 7:
		BarData = 1 << 14;
		break;

		case 8:
		BarData = 0b1000000000000001;
		break;
		
		case 9:
		BarData = 1 << 1; // 13
		break;
		
		case 10:
		BarData = 1 << 2; // 11
		break;
		
		case 11:
		BarData = 1 << 3; // 9
		break;
		
		case 12:
		BarData = 1 << 4; // 7
		break;
		
		case 13:
		BarData = 1 << 5;
		break;
		
		case 14:
		BarData = 1 << 6;
		break;
		
		case 15:
		BarData = 1 << 7;
		break;
		}
	if (BarNr == 1){
		BarData1 = BarData;
		}
	else {
		BarData2 = BarData;
		}

}

void BarKeuzeRoer(int Data,int BarNr){

	switch (Data){
	
		case 1: 
		BarData = 1 << 7;
		break;
		
		case 2:
		BarData = 1 << 6;
		break;
		
		case 3:
		BarData = 1 << 5;
		break;
		
		case 4:
		BarData = 1 << 4;
		break;
		
		case 5:
		BarData = 1 << 3;
		break;
		
		case 6:
		BarData = 1 << 2;
		break;
		
		case 7:
		BarData = 1 << 1;
		break;

		case 8:
		BarData = 0b1000000000000001;
		break;
		
		case 9:
		BarData = 1 << 14; // 13
		break;
		
		case 10:
		BarData = 1 << 13; // 11
		break;
		
		case 11:
		BarData = 1 << 12; // 9
		break;
		
		case 12:
		BarData = 1 << 11; // 7
		break;
		
		case 13:
		BarData = 1 << 10;
		break;
		
		case 14:
		BarData = 1 << 9;
		break;
		
		case 15:
		BarData = 1 << 8;
		break;
		}
	if (BarNr == 1){
		BarData3 = BarData;
		}
	else {
		BarData4 = BarData;
		}

}